//
//  LBLoadingTipsView.m
//  AFNetworking
//
//  Created by LINAICAI on 2018/1/29.
//

#import "LBLoadingFullView.h"
#import <Masonry.h>
#import <YLGIFImage/YLImageView.h>
#import <YLGIFImage/YLGIFImage.h>
#import <LBCategory/UIColor+LBColor.h>

@interface LBLoadingFullView()
@property (nonatomic , assign)LBLoadingFullViewStyle style;
@property (nonatomic , copy)retryCompleteHandler retry;
@end
@implementation LBLoadingFullView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [self initWithStyle:LBLoadingTipsViewError retry:nil];
    if (self) {

    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [self initWithStyle:LBLoadingTipsViewError retry:nil];
    if (self) {

    }
    return self;
}

- (instancetype)initWithStyle:(LBLoadingFullViewStyle)style retry:(retryCompleteHandler)retry
{
    self = [super initWithFrame:CGRectZero];
    if (self) {
        _style = style;
        _retry = [retry copy];
    }
    return self;
}
- (instancetype)initWithStyle:(LBLoadingFullViewStyle)style
{
   return [self initWithStyle:style retry:nil];
}
- (void)showInView:(UIView *)view{
    self.backgroundColor = [UIColor whiteColor];
    //容器层
    [view addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(view);
    }];
    //图片
    YLImageView *imageView = [[YLImageView alloc]init];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    //提示
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = [UIFont systemFontOfSize:20];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor colorWithRed:159/255.0 green:163/255.0 blue:166/255.0 alpha:1];
    titleLabel.numberOfLines = 0;
    //添加到容器并布局
    [self addSubview:imageView];
    [self addSubview:titleLabel];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.equalTo(self).dividedBy(1.5);
        make.size.mas_equalTo(CGSizeMake(200, 200));
    }];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(imageView.mas_bottom).mas_offset(10);
    }];
    switch (_style) {
        case LBLoadingTipsViewLoading:
        {
            titleLabel.text = @"正在努力加载中...";
            imageView.image = [YLGIFImage imageNamed:@"LBLoadingFullView.bundle/loading.gif"];
            
        }
            break;
        case LBLoadingTipsViewError:
        {
            titleLabel.text = @"网络异常或加载失败";
            imageView.image = [YLGIFImage imageNamed:@"LBLoadingFullView.bundle/loadingError.gif"];
            //点击重试按钮
            UIButton *retryButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [retryButton setTitle:@"点击重试" forState:UIControlStateNormal];
            [retryButton setTitleColor:[UIColor blue] forState:UIControlStateNormal];
            retryButton.titleLabel.font = [UIFont systemFontOfSize:20];
            [retryButton addTarget:self action:@selector(retryButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:retryButton];
            [retryButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(self);
                make.top.equalTo(titleLabel.mas_bottom).mas_offset(5);
            }];
            
        }
            break;
            
        default:
            break;
    }

    
}
- (void)retryButtonClick:(id)sender{
    [self dismiss];
    //暂时只有错误样式下才能触发重试的block
    if ((_style == LBLoadingTipsViewError) && _retry) {
        _retry(self);
    }
}
- (void)dismiss{
    [self removeFromSuperview];
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
